package com.jacky.iot.config;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.HashMap;
import java.util.Map;

public class MqttServerConfig {
    private final int port;
    private final String host;
    private final boolean enableSsl;
    private final boolean enableAuth;
    private final Map<String, String> users;
    private final String keystorePath;
    private final String keystorePassword;
    private final String protocolVersion;

    public MqttServerConfig(JsonObject config) {
        JsonObject mqttConfig = config.getJsonObject("mqtt");
        this.port = mqttConfig.getInteger("port", 1883);
        this.host = mqttConfig.getString("host", "0.0.0.0");
        this.enableSsl = mqttConfig.getBoolean("enableSsl", false);
        this.enableAuth = mqttConfig.getBoolean("enableAuth", false);
        this.keystorePath = mqttConfig.getString("keystorePath", "");
        this.keystorePassword = mqttConfig.getString("keystorePassword", "");
        this.protocolVersion = mqttConfig.getString("protocolVersion", "MQTT_5");
        this.users = new HashMap<>();

        if (enableAuth) {
            JsonArray usersArray = mqttConfig.getJsonArray("users");
            usersArray.forEach(item -> {
                JsonObject user = (JsonObject) item;
                users.put(user.getString("username"), user.getString("password"));
            });
        }
    }

    // Getters
    public boolean isEnableAuth() { return enableAuth; }
    public Map<String, String> getUsers() { return users; }

    // Getters
    public int getPort() { return port; }
    public String getHost() { return host; }
    public boolean isEnableSsl() { return enableSsl; }
    public String getKeystorePath() { return keystorePath; }
    public String getKeystorePassword() { return keystorePassword; }
    public String getProtocolVersion() { return protocolVersion; }
}
